package com.eleadmin.common.goods.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.eleadmin.common.core.web.ApiResult;
import com.eleadmin.common.core.web.BaseController;
import com.eleadmin.common.goods.domain.pojo.Goods;
import com.eleadmin.common.goods.domain.req.GoodsReq;
import com.eleadmin.common.goods.service.GoodsService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName GoodsController
 * @Description 描述
 * @Author ShiYu.Cui
 * @Date 2024/4/20 12:51
 */
@RestController
@RequestMapping("/api/goods")
public class GoodsController extends BaseController {
    @Autowired
    private GoodsService goodsService;


    @ApiOperation("查询所有商品信息")
    @PostMapping("/goodsList")
    public ApiResult<List<Goods>> goodsList(@RequestBody GoodsReq goodsReq) {
        return success(goodsService.goodsList(goodsReq));
    }

    @ApiOperation("添加商品")
    @PostMapping("/add")
    public ApiResult<?> add(@RequestBody Goods goods){
        if (goodsService.save(goods)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @ApiOperation("删除商品")
    @DeleteMapping("/{id}")
    public ApiResult<?> del(@PathVariable("id") Integer id) {
        if (goodsService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    /**
     * 导入
     * @param batchGoods
     * @return
     */
    @ApiOperation("批量添加商品")
    @PostMapping("batchGoods")
    public ApiResult<?> batchGoods(@RequestBody List<List<Object>> batchGoods) {
        ArrayList<Goods> goods1 = new ArrayList<>();
        for (List<Object> good : batchGoods) {
            Goods goods = new Goods();
            goods.setGoodId(good.get(1).toString());
            goods.setTitle(good.get(2).toString());
            goods.setPrice(Double.parseDouble(good.get(4).toString()));
            goods.setGoodType(good.get(3).toString());
            goods1.add(goods);
        }
        if (goodsService.saveBatch(goods1)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

}
